home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / replace.jav < prev    next >
Encoding:
Text File  |  1996-02-26  |  2.8 KB  |  97 lines

  1. /*
  2.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. /**
  18.  * This file defines the main test program which instantiates some
  19.  * file objects and then exercises some of their methods. The
  20.  * example implements a Replace object that substitutes one character
  21.  * in the input stream with another character. Both caracters are
  22.  * supplied as arguments to the Replace example.
  23.  */
  24.  
  25. class Replace {
  26.  
  27.     public static void Usage() {
  28.     System.out.println("\nUsage:  java Replace char1 char2 inFile outFile");
  29.     }
  30.  
  31.     public static void main(String args[]) {
  32.     InputFile    in = null;
  33.         OutputFile    out = null;
  34.         char        former = 'A';
  35.         char        latter = 'A';
  36.        byte        buf[];
  37.  
  38.     try {
  39.            former = args[0].charAt(0);
  40.     } 
  41.         catch (ArrayIndexOutOfBoundsException e) {
  42.         Usage();
  43.         System.out.println("you must supply the character to replace\n");
  44.         System.exit(-1);
  45.     }
  46.  
  47.     try {
  48.            latter = args[1].charAt(0);
  49.     } 
  50.         catch (ArrayIndexOutOfBoundsException e) {
  51.         Usage();
  52.         System.out.println("you must supply the new character\n");
  53.         System.exit(-1);
  54.     }
  55.  
  56.     try {
  57.         in = new InputFile(args[2]);
  58.     } 
  59.         catch (ArrayIndexOutOfBoundsException e) {
  60.         Usage();
  61.         System.out.println("you must supply the input replacement file\n");
  62.         System.exit(-1);
  63.     }
  64.  
  65.     try {
  66.         out = new OutputFile(args[3]);
  67.     } 
  68.         catch (ArrayIndexOutOfBoundsException e) {
  69.         Usage();
  70.         System.out.println("you must supply the output replacement file\n");
  71.         System.exit(-1);
  72.     }
  73.  
  74.     System.out.println("Replacing "+args[0]+" with "+args[1]+" from "+
  75.                args[2]+" to "+args[3]);
  76.  
  77.     if (in.open() == false) {
  78.         System.out.println("Unable to open input file "+in.getFileName());
  79.     }
  80.  
  81.     if (out.open() == false) {
  82.         System.out.println("Unable to open output file "+out.getFileName());
  83.     }
  84.  
  85.     buf = new byte[1];
  86.     while (in.read(buf, 1) == 1) {
  87.         if (buf[0] == former)
  88.         buf[0] = (byte)latter;
  89.         if (out.write(buf, 1) != 1) {
  90.         System.out.println("Error writing to "+out.getFileName());
  91.         }
  92.     }
  93.      in.close();
  94.      out.close();
  95.     }
  96. }
  97.